package Server.DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import VWorld.Avatar;
import VWorld.Message;
import VWorld.Room;

public class MessageDao extends VWorldDao{
	
	public static boolean insertMessage(Message message){
		connection();
		try{
			
				
				String requete = "INSERT INTO Message (Message.message_id, Message.from, Message.to, Message.room_id, Message.message,  Message.read) VALUES (NULL, '"+message.from.avatar_id+"', '"+message.to.avatar_id+"', '"+message.room.room_id+"','"+message.message+"', '0')";
				System.out.println(requete);
				PreparedStatement stmt = con.prepareStatement(requete);
				stmt.executeUpdate();
				stmt.close();
				System.out.println("Message sent to "+message.to.name);
			} catch (SQLException e) {
				System.out.println("ERROR: message not sent to"+message.to.name);
				e.printStackTrace();
			}
		
		return true;
		
	}
	
	public static Message[] getMessagesRoom(Room room){
		connection();
		Statement stm, stm2;
		ArrayList<Message> messages = new ArrayList<Message>();
		try {
			stm = con.createStatement();
			
			stm.execute ("SELECT Message.message_id, Message.from, Message.to, Message.message, Message.read  FROM Message WHERE Message.room_id='"+room.room_id+"'"); 
			ResultSet rs = stm.getResultSet();
			boolean read;
			int i =0;
			while(rs.next()){
				stm2 = con.createStatement();
				stm2.execute ("SELECT avatar_id, height, humor, sex, name, password, email, isAdmin, currentRoomId FROM Avatar WHERE avatar_id='"+rs.getInt("from")+"'"); 
				ResultSet rs2 = stm2.getResultSet();
				boolean isAdmin;
				rs2.next();

					
					if(rs2.getInt("isAdmin")==0){
						isAdmin= false;
					}else{
						isAdmin= true;
					}
				
					Avatar av = new Avatar(rs2.getInt("avatar_id"),
							VWorld.Height.from_int(rs2.getInt("height")), 
							VWorld.Humor.from_int(rs2.getInt("humor")), 
							VWorld.Sex.from_int(rs2.getInt("sex")), 
							rs2.getString("name"), 
							rs2.getString("password"), 
							rs2.getString("email"), 
							isAdmin, 
							rs2.getInt("currentRoomId"),
							true
						);
				
				if(rs.getInt("read")==0){
					read= false;
				}else{
					read= true;
				}
			
				Message message = new Message(rs.getInt("message_id"),
						av,
						new Avatar(rs2.getInt("avatar_id"),
								null, 
								null, 
								null, 
								null, 
								null, 
								null, 
								isAdmin, 
								rs2.getInt("currentRoomId"),
								true
							),
						room,
						rs.getString("message"),
						false
				);
				messages.add(message);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Message[] mess = new Message[messages.size()];
		
		
		return messages.toArray(mess) ;
	}
	
	public static Message[] getUnreadMessages(Avatar avatar){
		connection();
		Statement stm, stm2;
		ArrayList<Message> messages = new ArrayList<Message>();
		try {
			stm = con.createStatement();
			
			stm.execute ("SELECT Message.message_id, Message.from, Message.to, Message.message, Message.read  FROM Message WHERE Message.to='"+avatar.avatar_id+"' AND Message.read='0'"); 
			ResultSet rs = stm.getResultSet();
			boolean read;
			int i =0;
			while(rs.next()){
				stm2 = con.createStatement();
				stm2.execute ("SELECT avatar_id, height, humor, sex, name, password, email, isAdmin, currentRoomId FROM Avatar WHERE avatar_id='"+rs.getInt("from")+"'"); 
				ResultSet rs2 = stm2.getResultSet();
				boolean isAdmin;
				rs2.next();

					
					if(rs2.getInt("isAdmin")==0){
						isAdmin= false;
					}else{
						isAdmin= true;
					}
				
					Avatar av = new Avatar(rs2.getInt("avatar_id"),
							VWorld.Height.from_int(rs2.getInt("height")), 
							VWorld.Humor.from_int(rs2.getInt("humor")), 
							VWorld.Sex.from_int(rs2.getInt("sex")), 
							rs2.getString("name"), 
							rs2.getString("password"), 
							rs2.getString("email"), 
							isAdmin, 
							rs2.getInt("currentRoomId"),
							true
						);
				
				if(rs.getInt("read")==0){
					read= false;
				}else{
					read= true;
				}
			
				Message message = new Message(rs.getInt("message_id"),
						av,
						avatar,
						null,
						rs.getString("message"),
						false
				);
			}
			
		} catch (SQLException e) {
			System.out.println("getRoom(id)");
			e.printStackTrace();
		}
		Message[] mess = new Message[messages.size()];
		
		
		return messages.toArray(mess) ;
		
	}

}
